import 'package:flutter/material.dart';
import 'package:flutter_github_login/http_utils.dart';
import 'package:flutter_github_login/localstorage.dart';
import 'package:flutter_github_login/progressdialog.dart';
import 'package:flutter_github_login/routes.dart';
import 'package:get/get.dart';

import '../../common_toast.dart';
import '../../models/user.dart';

class LoginPage extends StatefulWidget {
  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  bool showPassword = false;
  String userName = "";
  String password = "";

  final TextEditingController userController = TextEditingController();
  final TextEditingController pwdController = TextEditingController();

  @override
  void initState() {
    super.initState();
    LocalStorage.initSP();
    initParams();
  }

  initParams() async {
    userController.value = TextEditingValue(text: userName);
    pwdController.value = TextEditingValue(text: password);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        iconTheme: IconThemeData(
          color: Colors.black, //修改颜色
        ),
        backgroundColor: Colors.white,
        title: const Text('登录', style: TextStyle(color: Colors.black)),
        centerTitle: true,
      ),
      body: SafeArea(
        minimum: const EdgeInsets.all(30),
        child: ListView(
          children: [
            TextField(
              decoration: InputDecoration(labelText: '用户名', hintText: '请输入用户名'),
              controller: userController,
              onChanged: (String value) {
                userName = value;
              },
            ),
            const Padding(padding: EdgeInsets.all(10)),
            /* TextField(
              decoration: InputDecoration(
                  labelText: '密码',
                  hintText: '请输入密码',
                  suffixIcon: IconButton(
                      onPressed: () {
                        setState(() {
                          showPassword = !showPassword;
                        });
                      },
                      icon: Icon(showPassword
                          ? Icons.visibility
                          : Icons.visibility_off))),
              obscureText: !showPassword,
              controller: pwdController,
              onChanged: (String value) {
                password = value;
              },
            ),*/
            const Padding(padding: EdgeInsets.all(10)),
            SizedBox(
              height: 50,
              width: double.infinity,
              child: ElevatedButton(
                child: const Text("登录"),
                onPressed: () {
                  if (userName.isEmpty) {
                    CommonToast.showToast("用户名不能为空");
                    return;
                  } else {
                    CommonToast.showToast("请稍后...");

                    HttpUtils.instance
                        .get('https://api.github.com/users/$userName')
                        .then((value) => {
                              if (User.fromJson(value).login!.isNotEmpty)
                                {
                                  LocalStorage.save("user", value.toString()),
                                  Navigator.pushReplacementNamed(
                                      context, Routes.home)
                                }
                            });
                  }
                },
              ),
            ),
            const Padding(padding: EdgeInsets.all(10)),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                const Text('还没有账号，'),
                TextButton(
                    child: const Text('去注册~'),
                    onPressed: () {
                      //Navigator.pushReplacementNamed(context, Routes.register);
                    }),
              ],
            )
          ],
        ),
      ),
    );
  }
}
